<?php
/**
 * Created by PhpStorm.
 * User: lhl
 * Date: 2020/10/16
 * Time: 15:29
 * Features: 后台权限管理
 */

namespace app\adminapiv3\controller\platform_manage\account;

use app\adminapiv3\controller\BaseController;
use app\adminapiv3\model\ServerPermissionModel;
use think\Request;

class ServerPermissionController extends BaseController
{
    //本控制器接口不与前端对接,也不受权限控制,避免外部调用,验证permissiontoken
    public function initialize()
    {
        parent::initialize();
        $token = \think\facade\Request::header('permissiontoken');
        if($token != 'BEF8E2BCB5A951BBDB8EE39079C4ADF3'){
            returnErrJson(105,'拒绝访问');
        }
    }

    /**
     * 列表
    */
    public function index(Request $request){
        //尝试创建顶级权限
        $model = new ServerPermissionModel();
        $model->createTopAuth();
        $serach = $request->only([
            'name','is_menu','route','redirect_url','menu_name'
        ],'get');

        $isRecursive = $model->isRecursive($serach);
        $list = $model->getList(true,false,$serach,$isRecursive);
        return $this->jsonOutPut($list);
    }

    /**
     * 添加
    */
    public function add(Request $request){
        $model = new ServerPermissionModel();
        //回显
        if($request->isGet()){
            $auths = $model->getList(false,true,[],true);
            return $this->jsonOutPut(['auths' => $auths]);
        }
        if($request->isPost()){
            $param = $request->only([
                'pid','name','is_menu','icon','route' => '','redirect_url','menu_name','sorts'
            ],'post');
            $scene = $model->getValidateScene($param,'add');
            $this->validate($param,'ServerPermission.'.$scene);
            //添加
            $res = $model->addAuth($param);
            return $this->jsonOutPut($res);
        }
    }

    /**
     * 修改
    */
    public function modify(Request $request){
        $model = new ServerPermissionModel();
        //回显
        if($request->isGet()){
            $param = $request->only('id','get');
            $this->validate($param,'ServerPermission.info');
            //所有权限数据
            $data['auths'] = $model->getList(false,true,[],true);
            $data['info'] = $model->getAuthById($param['id']);
            return $this->jsonOutPut($data);
        }
        if($request->isPost()){
            $param = $request->only([
                'id','pid','name','is_menu','icon','route','redirect_url','menu_name','sorts'
            ],'post');
            $scene = $model->getValidateScene($param,'modify');
            $this->validate($param,'ServerPermission.'.$scene);
            //修改
            $res = $model->modifyAuth($param);
            return $this->jsonOutPut($res);
        }
    }

    /**
     * 删除
    */
    public function del(Request $request){
        $param = $request->only(['id'],'post');
        $this->validate($param,'ServerPermission.del');
        $model = new ServerPermissionModel();
        $res = $model->del($param['id']);
        return $this->jsonOutPut($res);
    }

}